package edu.lingnan.lmlibrary.util;

import edu.lingnan.lmlibrary.common.HttpComponentsClientRestfulHttpRequestFactory;
import net.minidev.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

public class HttpRequestRouteStudentInfo {
    public static ResponseEntity<JSONObject> httpRequestRouteStudentInfo(String url, String method, Map<String,String> bodyMap, RestTemplate template){
        MultiValueMap<String, String> map = new HttpHeaders();
        map.add("token","eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxMDAwMCJ9.1_p7RIINRzcUiL_nFZEiwr6yTrVDX8noIbwLoAJw7V0");
        ResponseEntity<JSONObject> exchange = null;
        HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(bodyMap, map);
        if(method.equals("get")){
            template.setRequestFactory(new HttpComponentsClientRestfulHttpRequestFactory());
            exchange = template.exchange(url,
                    HttpMethod.GET, httpEntity, JSONObject.class);
        }
        else if(method.equals("post"))
        {
            exchange = template.exchange(url,
                    HttpMethod.POST, httpEntity, JSONObject.class);
        }
//        JSONObject body = exchange.getBody();
        return exchange;
    }
}
